//
//  FrdRjctMsgViewController.m
//  SIPphone
//
//  Created by wrt on 13-11-28.
//  Copyright (c) 2013年 wrtsoft. All rights reserved.
//

#import "FrdRjctMsgViewController.h"
#import "ContactsDateBase.h"
#import "SIPInterface.h"

@interface FrdRjctMsgViewController ()
{
    NSString *tempUserName;
    NSString *tempDisplayName;
    NSString *tempIpPort;
}

@end

@implementation FrdRjctMsgViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"拒绝信息";
    }
    return self;
}

- (id)initWithUserName:(NSString *)userName andDisplayName:(NSString *)displayName andipPort:(NSString *)ip
{
    self = [super init];
    if (self)
    {
        tempUserName = [[NSString alloc] init];
        tempDisplayName = [[NSString alloc] init];
        tempIpPort = [[NSString alloc] init];
        tempUserName = userName;
        tempDisplayName = displayName;
        tempIpPort = ip;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.userNameLabel.text = tempUserName;
    self.displayNameLabel.text = tempDisplayName;
    self.ipportLabel.text = tempIpPort;
    
}
- (IBAction)againAddFriendButtonPressed:(id)sender
{
    
    //先判断是否是登陆状态，如果不是登陆状态则提示请登陆
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL registerStatus = [defaults boolForKey:@"REGISTERSTATUS"];
    if (registerStatus)
    {
        
        ContactsDateBase *contactsDB = [ContactsDateBase shareContactsDataBase];
        for (int i = 0 ; i < contactsDB.userNameArray.count ; i++)
        {
            if ([tempUserName isEqualToString:contactsDB.userNameArray[i]] && [tempIpPort isEqualToString:contactsDB.ipPortArray[i]])
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"通知" message:@"已经添加过该域名下的该用户\n请勿重复添加..." delegate:self cancelButtonTitle:@"好的" otherButtonTitles: nil];
                [alert show];
                return;
            }
            else if([tempUserName isEqualToString:contactsDB.markNameArray[i]])
            {
                //数据库进行更新
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"通知" message:@"添加的用户在联系人名单中存在相同的备注名，为了避免数据出错，请修改相同名字的备注名再添加，谢谢合作" delegate:self cancelButtonTitle:@"好的" otherButtonTitles: nil];
                [alert show];
                return;
            }
        }
        
//        [[SIPInterface shareSIPInterface] addFriendsWithUserName:tempUserName andDisPlayName:@"" andIp:tempIpPort];
        
        //放到联系人数据库内
        [contactsDB saveFriendIntoDBWithUserName:tempUserName andMarkName:@"" andIp:tempIpPort];
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"通知" message:@"好友请求已经发送" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
        [alert show];
        [self.navigationController popViewControllerAnimated:YES];
        [alert dismissWithClickedButtonIndex:0 animated:YES];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"通知" message:@"请先登陆" delegate:self cancelButtonTitle:@"明白了" otherButtonTitles: nil];
        [alert show];
        
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
